//
//  BaseTabBarController.m
//  SleepingElf
//
//  Created by karong on 2023/6/6.
//

#import "BaseTabBarController.h"
#import "BaseTabBar.h"
#import "SEDiscoverController.h"
#import "HomeController.h"
#import "SleepController.h"
#import "MeController.h"
#import "MusicController.h"

@interface BaseTabBarController ()<UITabBarControllerDelegate, BaseTabBarDelegate>

@property (nonatomic, strong) BaseTabBar *customTabBar;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    
    BaseTabBar *tabBar = [[BaseTabBar alloc]init];
    self.customTabBar = tabBar;
    self.customTabBar.delegate = self;
    self.customTabBar.tabDelegate = self;
    [self setValue:tabBar forKey:@"tabBar"];
    
    NSMutableArray * controllers = [NSMutableArray new];
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"HomeController" bundle:nil];
    HomeController *homeController = [storyBoard instantiateInitialViewController];
    BaseNavigationController * homeNav = [[BaseNavigationController alloc]initWithRootViewController:homeController];
    homeNav.navigationBarHidden = YES;
    [controllers addObject:homeNav];
    
    storyBoard = [UIStoryboard storyboardWithName:@"SleepController" bundle:nil];
    SleepController *sleepController = [storyBoard instantiateInitialViewController];
    BaseNavigationController * sleepNav = [[BaseNavigationController alloc]initWithRootViewController:sleepController];
    sleepNav.navigationBarHidden = YES;
    [controllers addObject:sleepNav];
    
    SEDiscoverController * emptyVC3 = [SEDiscoverController new];
    [controllers addObject:emptyVC3];
    
    storyBoard = [UIStoryboard storyboardWithName:@"MeController" bundle:nil];
    MeController *meController = [storyBoard instantiateInitialViewController];
    BaseNavigationController * meNav = [[BaseNavigationController alloc]initWithRootViewController:meController];
    meNav.navigationBarHidden = YES;
    [controllers addObject:meNav];
    
    [self setViewControllers:controllers];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger index = [tabBar.items indexOfObject:item];
    if (index != self.selectIndex) {
//        NSMutableArray * subView = [NSMutableArray new];
        NSMutableArray * itemView = [NSMutableArray new];
        for (UIView *tempView in tabBar.subviews) {
                if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                    [itemView addObject:tempView];
//                    for (UIImageView *tempImageView in tempView.subviews) {
//                        if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
//                            [subView addObject:tempImageView];
//                        }
//                    }
                    
                }
            }
//        UIView * subItem = subView[index];
//        [subItem addScaleAnimtaionWithfromValue:0.7 toValue:1.1 duration:0.15 repeatCount:1 autoreverses:YES];
        if (index == 2) {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MusicController" bundle:nil];
            MusicController *target = [storyBoard instantiateInitialViewController];
            target.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            target.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentViewController:target animated:YES completion:nil];
        }
        else {
            UIView *tempView = itemView[index];
            [UIView animateWithDuration:0.2 animations:^{
                self.customTabBar.slideImageView.centerX = tempView.centerX;
            }];
            self.selectIndex = index;
        }
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:NSClassFromString(@"SEDiscoverController")]) {
        return NO;
    }
    return YES;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    _selectIndex = selectedIndex;
    if (selectedIndex == 2) {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MusicController" bundle:nil];
        MusicController *target = [storyBoard instantiateInitialViewController];
        target.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        target.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:target animated:YES completion:nil];
    }
}

@end
